package com.bw.zg3.notice

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bw.base.entity.GGEntityResultItem
import com.bw.zg3.R
import com.bw.zg3.databinding.GgBinding

class NoticeApter :PagingDataAdapter<GGEntityResultItem,NoticeApter.NoticeViewHolder>(diff){
    private var mOnItemClickListener: OnItemClickListener? = null
    private var mOnItemLongClickListener: OnItemLongClickListener? = null
    companion object{
        val diff=object :androidx.recyclerview.widget.DiffUtil.ItemCallback<GGEntityResultItem>(){
            override fun areItemsTheSame(
                oldItem: GGEntityResultItem,
                newItem: GGEntityResultItem,
            ): Boolean {
                return oldItem==newItem
            }

            override fun areContentsTheSame(
                oldItem: GGEntityResultItem,
                newItem: GGEntityResultItem,
            ): Boolean {
                return oldItem.id==newItem.id
            }

        }
    }
    class NoticeViewHolder(view: View): RecyclerView.ViewHolder(view){
        val binding= DataBindingUtil.bind<GgBinding>(view)
    }

    override fun onBindViewHolder(holder: NoticeViewHolder, position: Int) {
        holder.binding?.bean=getItem(position)
        val item = getItem(position)
        holder.itemView.setOnClickListener {
            this.mOnItemClickListener?.onItemClick(position,item!!)
        }
        holder.itemView.setOnLongClickListener{
            this.mOnItemLongClickListener?.onItemLongClick(item!!)
            return@setOnLongClickListener true
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoticeViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.gg, parent, false)
        return NoticeViewHolder(view)
    }
     fun setOnItemClickListener(listener: OnItemClickListener?) {
        this.mOnItemClickListener = listener
    }

     fun setOnItemLongClickListener(listener: OnItemLongClickListener?) {
        this.mOnItemLongClickListener = listener
    }
}
interface OnItemClickListener {

    fun onItemClick(position: Int,item: GGEntityResultItem)
}
interface OnItemLongClickListener {

    fun onItemLongClick(item: GGEntityResultItem): Boolean
}
